package com.smzd.ccms.actions;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.smzd.ccms.business.ChannelService;
import com.smzd.ccms.business.SiteChannelConfigService;
import com.smzd.ccms.core.CompletPluginManager;
import com.smzd.ccms.domain.Channel;
import com.smzd.ccms.domain.Site;
import com.smzd.ccms.domain.SiteChannelConfig;
import com.smzd.ccms.domain.SysDict;
import com.smzd.framework.annotation.Access;
import com.smzd.framework.system.domain.User;
import com.smzd.framework.utils.MeetingSessionHolder;
import com.smzd.utils.SessionHolder;

@Controller
@RequestMapping("/a/{site}")
@Access("TAG_ROLE_CCMS")
public class AdminController {
	@Autowired
	private ChannelService channelService;
	@Autowired
	private SiteChannelConfigService sccService;

	/**
	 * 进入站点的后台管理页面。正式方案。
	 * 
	 * @param model
	 * @param request
	 * @param site
	 * @return
	 */

	@RequestMapping({ "index.html", "" })
	public String toPortalAdminSite(Map<String, Object> model, HttpServletRequest request,
			@PathVariable("site") String siteStr) {
		Site site = CompletPluginManager.getSite(siteStr);
		User loginUser = MeetingSessionHolder.getLoginUser();
		model.put("user", loginUser);
		model.put("_site_", siteStr);
		model.put("_site", site);
		List<Channel> channels = channelService.getAllChannelsByMeetingId(site.getOwner().getId());
		model.put("_channels", channels);
		return getMyView("index.ftl");
	}

	@RequestMapping("/profile.html")
	public String toSiteIndexPage(Map<String, Object> model, @PathVariable("site") String siteStr) {
		Site site = CompletPluginManager.getSite(siteStr);
		SessionHolder.setObject(Site.SESSION_KEY_CURRENT_SITE, site);// 这个是为了类似于登录的session设置，后面可能会取消这一系列的操作，都是用上面的语句来完成
		model.put("_site", site);
		model.put("_site_", siteStr);
		User user = MeetingSessionHolder.getLoginUser();
		model.put("user", user);
		SiteChannelConfig scc = sccService.getSiteChannelConfig(site.getOwner().getId(),
				SysDict.ID_SITE_CH_CONFIG_INDEX);
		model.put("scc", scc);
		return getMyView("site-profile.ftl");
	}

	private String getMyView(String view) {
		return "ccms/admin/" + view;
	}

}
